package com.compomics.prioritizer.db.accessors;

import java.util.HashMap;
import java.util.Vector;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.PreparedStatement;

/**
 * Created by IntelliJ IDEA.
 * User: Niklaas Colaert
 * Date: 7-nov-2008
 * Time: 10:26:53
 * To change this template use File | Settings | File Templates.
 */

public class Modification_link extends Modification_linkTableAccessor {

    /**
     * Default constructor.
     */
    public Modification_link() {
        super();
    }

    /**
     * Wrapper for the superclass constructor.
     *
     * @param aParams   HashMap with the parameters.
     */
    public Modification_link(HashMap aParams) {
        super(aParams);
    }

    /**
     * This constructor reads the modification_link from a resultset. The ResultSet should be positioned such that
     * a single row can be read directly (i.e., without calling the 'next()' method on the ResultSet). <br />
     * The columns should be in this order: <br />
     * Column 1: modification_link ID <br />
     * Column 2: project is 1 if the modifications for a project are stored <br />
     * Column 3: protocol is 1 if the modifications for a protocol are stored <br />
     * Column 4: type is 1 if its fixed , 0 if its variable <br />
     * Column 5: l_linkid <br/>
     * Column 6: l_modificationid <br/>
     * Column 7: username <br />
     * Column 8: creationdate <br />
     * Column 89: modificationdate.
     *
     * @param   aRS ResultSet to read the data from.
     * @exception java.sql.SQLException    when reading the ResultSet failed.
     */
    public Modification_link(ResultSet aRS) throws SQLException {
        iModification_linkid = aRS.getLong(1);
        iProject = aRS.getInt(2);
        iProtocol = aRS.getInt(3);
        iType = aRS.getInt(4);
        iL_linkid = aRS.getLong(5);
        iL_modificationid= aRS.getLong(6);
        iUsername = aRS.getString(7);
        iCreationdate = (java.sql.Timestamp)aRS.getObject(8);
        iModificationdate = (java.sql.Timestamp)aRS.getObject(9);
    }

    /**
     * This methods reads all modification_links from the Modification_link table.
     *
     * @param aConn Connection to read the modification_links from.
     * @return  Modification_link[] with the modification_links in the 'Modification_link' table.
     * @throws SQLException when the retrieving of the modification_links went wrong.
     */
    public static Modification_link[] getAllModification_links(Connection aConn) throws SQLException {
        PreparedStatement prep = aConn.prepareStatement("select * from modification_link");
        ResultSet rs = prep.executeQuery();
        Vector v = new Vector();
        while(rs.next()) {
            v.add(new Modification_link(rs));
        }
        rs.close();
        prep.close();
        Modification_link[] lModification_links = new Modification_link[v.size()];
        v.toArray(lModification_links);

        return lModification_links;
    }

    public static void deleteAllModificationsForProject(Connection aConn, long aProjectid) throws SQLException {
        PreparedStatement prep = aConn.prepareStatement("delete d from modification_link as d where l_linkid = ? and project = 1");
        prep.setLong(1,aProjectid);
        prep.executeUpdate();
    }



    /**
     * This methods reads all modification_links from the Modification_link table.
     *
     * @param aConn Connection to read the modification_links from.
     * @return  Modification_link[] with the modification_links in the 'Modification_link' table.
     * @throws SQLException when the retrieving of the modification_links went wrong.
     */
    public static Modification[] getProjectVariableModification_links(Connection aConn, Long aId) throws SQLException {
        PreparedStatement prep = aConn.prepareStatement("select * from modification_link where project = 1 and l_linkid = ? and type = 0");
        prep.setLong(1, aId);
        ResultSet rs = prep.executeQuery();
        Vector v = new Vector();
        while(rs.next()) {
            v.add(new Modification_link(rs));
        }
        rs.close();
        prep.close();
        Modification_link[] lModification_links = new Modification_link[v.size()];
        v.toArray(lModification_links);

        Vector m = new Vector();
        for(int l = 0; l<lModification_links.length; l ++){
            Modification mod = Modification.getModificationById(aConn, lModification_links[l].getL_modificationid());
            m.add(mod);
        }

        Modification[] lModifications = new Modification[m.size()];
        m.toArray(lModifications);
        return lModifications;
    }

    /**
     * This methods reads all modification_links from the Modification_link table.
     *
     * @param aConn Connection to read the modification_links from.
     * @return  Modification_link[] with the modification_links in the 'Modification_link' table.
     * @throws SQLException when the retrieving of the modification_links went wrong.
     */
    public static Modification[] getProjectFixedModification_links(Connection aConn, Long aId) throws SQLException {
        PreparedStatement prep = aConn.prepareStatement("select * from modification_link where project = 1 and l_linkid = ? and type = 1");
        prep.setLong(1, aId);
        ResultSet rs = prep.executeQuery();
        Vector v = new Vector();
        while(rs.next()) {
            v.add(new Modification_link(rs));
        }
        rs.close();
        prep.close();
        Modification_link[] lModification_links = new Modification_link[v.size()];
        v.toArray(lModification_links);

        Vector m = new Vector();
        for(int l = 0; l<lModification_links.length; l ++){
            Modification mod = Modification.getModificationById(aConn, lModification_links[l].getL_modificationid());
            m.add(mod);
        }

        Modification[] lModifications = new Modification[m.size()];
        m.toArray(lModifications);
        return lModifications;
    }

    /**
     * This methods reads all modification_links from the Modification_link table.
     *
     * @param aConn Connection to read the modification_links from.
     * @return  Modification_link[] with the modification_links in the 'Modification_link' table.
     * @throws SQLException when the retrieving of the modification_links went wrong.
     */
    public static Modification[] getProtocolVariableModification_links(Connection aConn, Long aId) throws SQLException {
        PreparedStatement prep = aConn.prepareStatement("select * from modification_link where protocol = 1 and l_linkid = ? and type = 0");
        prep.setLong(1, aId);
        ResultSet rs = prep.executeQuery();
        Vector v = new Vector();
        while(rs.next()) {
            v.add(new Modification_link(rs));
        }
        rs.close();
        prep.close();
        Modification_link[] lModification_links = new Modification_link[v.size()];
        v.toArray(lModification_links);

        Vector m = new Vector();
        for(int l = 0; l<lModification_links.length; l ++){
            Modification mod = Modification.getModificationById(aConn, lModification_links[l].getL_modificationid());
            m.add(mod);
        }

        Modification[] lModifications = new Modification[m.size()];
        m.toArray(lModifications);
        return lModifications;
    }

    /**
     * This methods reads all modification_links from the Modification_link table.
     *
     * @param aConn Connection to read the modification_links from.
     * @return  Modification_link[] with the modification_links in the 'Modification_link' table.
     * @throws SQLException when the retrieving of the modification_links went wrong.
     */
    public static Modification[] getProtocolFixedModification_links(Connection aConn, Long aId) throws SQLException {
        PreparedStatement prep = aConn.prepareStatement("select * from modification_link where protocol = 1 and l_linkid = ? and type = 1");
        prep.setLong(1, aId);
        ResultSet rs = prep.executeQuery();
        Vector v = new Vector();
        while(rs.next()) {
            v.add(new Modification_link(rs));
        }
        rs.close();
        prep.close();
        Modification_link[] lModification_links = new Modification_link[v.size()];
        v.toArray(lModification_links);

        Vector m = new Vector();
        for(int l = 0; l<lModification_links.length; l ++){
            Modification mod = Modification.getModificationById(aConn, lModification_links[l].getL_modificationid());
            m.add(mod);
        }

        Modification[] lModifications = new Modification[m.size()];
        m.toArray(lModifications);
        return lModifications;
    }

    /**
     * Returns a String representation for this Modification_link.
     *
     * @return  String  with the String representation for this Modification_link.
     */
    public String toString() {
        return String.valueOf(this.iModification_linkid);
    }


}
